package de.dhbw.asa.model;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;


@ManagedBean
@RequestScoped
public class ProductType extends Database {
	
	int iProductTypeNr;
	int iPrice;
	String sTitle;
	String sDescription;
	
	/*
	 * loads an existing Products
	 */
	public boolean load(int iProductTypeNr)
	{
		boolean bReturn = false;
		String sProductTypeNr = String.valueOf(iProductTypeNr);
		StringBuffer sbQuery = new StringBuffer();
		sbQuery.append("Select * from ProductType where ProductTypeNr = '");
		sbQuery.append(sProductTypeNr);
		sbQuery.append("';");
		try
		{
			if (this.createConnection())
			{
				ResultSet jResult = this.executeQuery(sbQuery.toString());
				if (jResult.next())
				{
					this.iProductTypeNr = iProductTypeNr;
					this.sTitle = jResult.getString("title");
					this.sDescription = jResult.getString("description");

				}
				bReturn = true;
				this.closeConnection();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return bReturn;
	}
	
	
	/*
	 * prepares a list with all available product types
	 */
	public List<ProductType> loadAllProductTypes()
	{
		List<ProductType> jPTypeList = new ArrayList<ProductType>();
		try
		{
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("Select * from ProductType where iProductTypeNr =");
			sbQuery.append(this.iProductTypeNr);
			sbQuery.append(";");
			ResultSet jResult = this.executeQuery(sbQuery.toString());
			while (jResult.next())
			{
				ProductType jProductType = new ProductType();
				jProductType.load(jResult.getInt("ProductNr"));
				jPTypeList.add(jProductType);
			}
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return jPTypeList;
	}
	
	/*
	 * Saves the changes of a product type
	 */
	public String saveProductChanges()
	{
		String sReturn = "";
		try
		{
			this.createConnection();
			StringBuffer sbQuery2 = new StringBuffer();
			// Insert the product type into the Database
			sbQuery2.append("update producttype set price='");
			sbQuery2.append(this.iPrice);
			sbQuery2.append("', description ='");
			sbQuery2.append(this.sDescription);
			sbQuery2.append("', title ='");
			sbQuery2.append(this.sTitle);
			sbQuery2.append("' where producttypenr =");
			sbQuery2.append(this.iProductTypeNr);
			sbQuery2.append(";");
			this.execute(sbQuery2.toString());
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			sReturn = e.getMessage();
		}
		return sReturn;
	}
	
	/*
	 * saves a new Customer in the Database
	 */
	public String saveNewProductType(String sTitle, int iPrice, String sDescription)
	{
		String sReturn = "";
		
		this.sTitle = sTitle;
		this.iPrice = iPrice;
		this.sDescription = sDescription;

		try
		{
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			// Finding out the last ProductTypeNr
			sbQuery.append("Select productTypeNr from productType;");
			ResultSet jResult = this.executeQuery(sbQuery.toString());
			int iLastProductTypeNr = 0;
			while (jResult.next())
			{
				iLastProductTypeNr = jResult.getInt("productTypeNr");
			}
			this.iProductTypeNr = iLastProductTypeNr+1;
			StringBuffer sbQuery2 = new StringBuffer();
			// Insert the product type into the Database
			sbQuery2.append("insert into producttype (productTypeNr, title, prise, description) values (");
			sbQuery2.append(this.iProductTypeNr);
			sbQuery2.append(",'");
			sbQuery2.append(this.sTitle);
			sbQuery2.append("','");
			sbQuery2.append(this.iPrice);
			sbQuery2.append("','");
			sbQuery2.append(this.sDescription);
			sbQuery2.append("';");
			this.execute(sbQuery2.toString());
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			sReturn = e.getMessage();
		}
		return sReturn;
	}
	
	
	
	public int getiProductTypeNr() {
		return iProductTypeNr;
	}
	public void setiProductTypeNr(int iProductTypeNr) {
		this.iProductTypeNr = iProductTypeNr;
	}
	public int getiPrice() {
		return iPrice;
	}
	public void setiPrice(int iPrice) {
		this.iPrice = iPrice;
	}
	public String getsTitle() {
		return sTitle;
	}
	public void setsTitle(String sTitle) {
		this.sTitle = sTitle;
	}
	public String getsDescription() {
		return sDescription;
	}
	public void setsDescription(String sDescription) {
		this.sDescription = sDescription;
	}
		

}
